package com.example.controller;

import cn.dev33.satoken.util.SaResult;
import com.example.entity.po.User;
import com.example.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
@ApiSupport(author = "泽")
@Tag(name = "用户类", description = "用户的CURD，登录，退出")
public class UserController {

	@Autowired
	private UserService userService;

	@Operation(summary = "获取所有用户信息")
	@GetMapping
	public SaResult getUsers() {
		return SaResult.data(userService.list());
	}

	@Operation(summary = "根据id查询用户信息")
	@Parameters({
			@Parameter(name = "id", description = "用户id", in = ParameterIn.PATH),
	})
	@GetMapping("{id}")
	public SaResult getUserById(@PathVariable String id) {
		return SaResult.data(userService.getById(id));
	}

	@Operation(summary = "更新用户信息")
	@PostMapping
	public SaResult updateUser(@RequestBody User user) {
		userService.updateUserById(user);
		return SaResult.ok();
	}

	@Operation(summary = "获取用户首页信息")
	@GetMapping("getUserInfo")
	public SaResult getUserInfo(@RequestHeader("token") String token) {
		return SaResult.data(userService.getUserInfo(token));
	}

	@Operation(summary = "用户登录")
	@Parameters({
			@Parameter(name = "loginCode", description = "微信返回的临时code", in = ParameterIn.QUERY, required = true),
			@Parameter(name = "token", description = "token", in = ParameterIn.HEADER)
	})
	@GetMapping("login")
	public SaResult login(@RequestParam("loginCode") String loginCode, @RequestHeader(value = "token", required =
			false) String token) {
		Map<String, String> message = userService.login(loginCode, token);
		return SaResult.data(message);
	}

}